home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Toolkit / Songbird 0.1 / Songbird_0_1_0.exe / components / sbISmartPlaylist.js < prev    next >
Text File  |  2006-02-07  |  30KB  |  1,063 lines

  1. /*
  2.  //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright⌐ 2006 Pioneers of the Inevitable LLC
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the ôGPLö).
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an ôAS ISö basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. //
  28. // sbISmartPlaylist Object
  29. //
  30.  
  31. const SONGBIRD_PLAYLIST_IID = Components.interfaces.sbIPlaylist;
  32.  
  33. const SONGBIRD_SMARTPLAYLIST_CONTRACTID = "@songbird.org/Songbird/SmartPlaylist;1";
  34. const SONGBIRD_SMARTPLAYLIST_CLASSNAME = "Songbird Smart Playlist Interface"
  35. const SONGBIRD_SMARTPLAYLIST_CID = Components.ID('{07BC8B9F-FB08-4fd6-92A4-9D5CDFE2D823}');
  36. const SONGBIRD_SMARTPLAYLIST_IID = Components.interfaces.sbISmartPlaylist;
  37.  
  38. const SMARTPLAYLIST_LIST_TABLE_NAME = "smartplaylist_list";
  39.  
  40. function CSmartPlaylist()
  41. {
  42.   var query = Components.classes["@songbird.org/Songbird/DatabaseQuery;1"].createInstance();
  43.   query = query.QueryInterface(Components.interfaces.sbIDatabaseQuery);
  44.  
  45.   this.m_internalQueryObject = query;
  46. }
  47.  
  48. CSmartPlaylist.prototype.constructor = CSmartPlaylist;
  49.  
  50. /* the CPlaylist class def */
  51. CSmartPlaylist.prototype = 
  52. {
  53.   m_strName: "",
  54.   m_strReadableName: "",
  55.   m_queryObject: null,
  56.   m_internalQueryObject: null,
  57.   m_strQuery: "",
  58.   
  59.   SetQueryObject: function(queryObj)
  60.   {
  61.     this.m_queryObject = queryObj;
  62.     this.m_internalQueryObject.SetDatabaseGUID(queryObj.GetDatabaseGUID());
  63.     return;
  64.   },
  65.   
  66.   GetQueryObject: function()
  67.   {
  68.     return this.m_queryObject;
  69.   },
  70.   
  71.   AddByGUID: function(mediaGUID, serviceGUID, nPosition, bReplace, bWillRunLater)
  72.   {
  73.     return false;
  74.   },
  75.   
  76.   RemoveByGUID: function(mediaGUID, bWillRunLater)
  77.   {
  78.     return false;
  79.   },
  80.   
  81.   RemoveByIndex: function(mediaIndex, bWillRunLater)
  82.   {
  83.     if(this.m_queryObject != null)
  84.     {
  85.       if(!bWillRunLater)
  86.         this.m_queryObject.ResetQuery();
  87.  
  88.       this.m_queryObject.AddQuery("DELETE FROM \"" + this.m_strName + "\" WHERE playlist_id = \"" + mediaIndex + "\"");
  89.       
  90.       if(!bWillRunLater)
  91.       {
  92.         this.m_queryObject.Execute();
  93.         this.m_queryObject.WaitForCompletion();
  94.       }
  95.       
  96.       return true;
  97.     }
  98.     
  99.     return false;
  100.   },
  101.   
  102.   MoveByGUID: function(mediaGUID, nPosition)
  103.   {
  104.     return false;
  105.   },
  106.   
  107.   MoveByIndex: function(mediaIndex, nPosition)
  108.   {
  109.     return false;
  110.   },
  111.  
  112.   FindByGUID: function(mediaGUID)
  113.   {
  114.     if(this.m_internalQueryObject != null)
  115.     {
  116.       this.m_internalQueryObject.ResetQuery();
  117.       this.m_internalQueryObject.AddQuery("SELECT id FROM \"" + this.m_strName + "\" WHERE playlist_uuid = \"" + mediaGUID + "\"");
  118.       
  119.       this.m_internalQueryObject.Execute();
  120.       this.m_internalQueryObject.WaitForCompletion();
  121.       
  122.       var resObj = this.m_internalQueryObject.GetResultObject();
  123.       if(resObj.GetRowCount())
  124.         return resObj.GetRowCell(0, 0);
  125.     }
  126.     
  127.     return -1;
  128.   },
  129.   
  130.   FindByIndex: function(mediaIndex)
  131.   {
  132.     if(this.m_internalQueryObject != null)
  133.     {
  134.       this.m_internalQueryObject.ResetQuery();
  135.       this.m_internalQueryObject.AddQuery("SELECT uuid FROM \"" + this.m_strName + "\" WHERE playlist_id = \"" + nIndex + "\"");
  136.       
  137.       this.m_internalQueryObject.Execute();
  138.       this.m_internalQueryObject.WaitForCompletion();
  139.       
  140.       var resObj = this.m_internalQueryObject.GetResultObject();
  141.       if(resObj.GetRowCount())
  142.         return resObj.GetRowCell(0, 0);
  143.     }
  144.     
  145.     return "";
  146.   },
  147.  
  148.   GetColumnInfo: function()
  149.   {
  150.     if(this.m_queryObject != null)
  151.     {
  152.       this.m_queryObject.ResetQuery();
  153.       this.m_queryObject.AddQuery("SELECT * FROM library_desc");
  154.       
  155.       this.m_queryObject.Execute();
  156.       this.m_queryObject.WaitForCompletion();
  157.     }
  158.   },
  159.   
  160.   SetColumnInfo: function(strColumn, strReadableName, isVisible, defaultVisibility, isMetadata, sortWeight, colWidth, bWillRunLater)
  161.   {
  162.     if(this.m_queryObject != null)
  163.     {
  164.       if(!bWillRunLater)
  165.         this.m_queryObject.ResetQuery();
  166.       
  167.       var strQuery = "UPDATE \"" + this.m_strName + "_desc\" SET ";
  168.       strQuery += "readable_name = \"" + strReadableName + "\", ";
  169.       strQuery += "is_visible = \"" + isVisible ? 1: 0 + "\", ";
  170.       strQuery += "default_visibility = \"" + defaultVisibility ? 1 : 0 + "\", ";
  171.       strQuery += "is_metadata = \"" + isMetadata ? 1 : 0 + "\", ";
  172.       strQuery += "sort_weight = \"" + sortWeight + "\", ";
  173.       strQuery += "width = \"" + colWidth + "\" ";
  174.       strQuery += "WHERE column_name = \"" + strColumn + "\"";
  175.       
  176.       this.m_queryObject.AddQuery(strQuery);
  177.       
  178.       strQuery = "UPDATE \"library_desc\" SET ";
  179.       strQuery += "readable_name = \"" + strReadableName + "\", ";
  180.       strQuery += "is_visible = \"" + isVisible ? 1: 0 + "\", ";
  181.       strQuery += "default_visibility = \"" + defaultVisibility ? 1 : 0 + "\", ";
  182.       strQuery += "is_metadata = \"" + isMetadata ? 1 : 0 + "\", ";
  183.       strQuery += "sort_weight = \"" + sortWeight + "\", ";
  184.       strQuery += "width = \"" + colWidth + "\" ";
  185.       strQuery += "WHERE column_name = \"" + strColumn + "\"";
  186.       
  187.       this.m_queryObject.AddQuery(strQuery);
  188.       
  189.       if(!bWillRunLater)
  190.       {
  191.         this.m_queryObject.Execute();
  192.         this.m_queryObject.WaitForCompletion();
  193.       }
  194.     }    
  195.   },
  196.  
  197.   GetTableInfo: function()
  198.   {
  199.     if(this.m_queryObject != null)
  200.     {
  201.       this.m_queryObject.ResetQuery();
  202.       this.m_queryObject.AddQuery("PRAGMA table_info(\"" + this.m_strName + "\")");
  203.       
  204.       this.m_queryObject.Execute();
  205.       this.m_queryObject.WaitForCompletion();
  206.     }
  207.   },
  208.   
  209.   AddColumn: function(strColumn, strDataType)
  210.   {
  211.     return;
  212.   },
  213.   
  214.   DeleteColumn: function(strColumn)
  215.   {
  216.     return;
  217.   },
  218.  
  219.   GetNumEntries: function()
  220.   {
  221.     if(this.m_queryObject != null)
  222.     {
  223.       this.m_queryObject.ResetQuery();
  224.       this.m_queryObject.AddQuery("SELECT COUNT(id) FROM \"" + this.m_strName + "\"");
  225.       
  226.       this.m_queryObject.Execute();
  227.       this.m_queryObject.WaitForCompletion();
  228.       
  229.       var resObj = this.m_queryObject.GetResultObject();
  230.       
  231.       return resObj.GetRowCell(0, 0);
  232.     }
  233.     
  234.     return 0;
  235.   },  
  236.   
  237.   GetEntry: function(nEntry)
  238.   {
  239.     if(this.m_queryObject != null)
  240.     {
  241.       this.m_queryObject.ResetQuery();
  242.       this.m_queryObject.AddQuery("SELECT * FROM \"" + this.m_strName + "\" WHERE id = \"" + nEntry + "\"");
  243.       
  244.       this.m_queryObject.Execute();
  245.       this.m_queryObject.WaitForCompletion();
  246.  
  247.       return 1;
  248.     }
  249.     
  250.     return 0;
  251.   },
  252.  
  253.   GetAllEntries: function()
  254.   {
  255.     if(this.m_queryObject != null)
  256.     {
  257.       this.m_queryObject.ResetQuery();
  258.       this.m_queryObject.AddQuery("SELECT * FROM \"" + this.m_strName + "\"");
  259.       
  260.       this.m_queryObject.Execute();
  261.       this.m_queryObject.WaitForCompletion();
  262.       
  263.       var resObj = this.m_queryObject.GetResultObject();
  264.       return resObj.GetRowCount();
  265.     }
  266.     
  267.     return 0;
  268.   },
  269.  
  270.   GetColumnValueByIndex: function(mediaIndex, strColumn)
  271.   {
  272.     if(this.m_queryObject != null)
  273.     {
  274.       this.m_queryObject.ResetQuery();
  275.       this.m_queryObject.AddQuery("SELECT " + strColumn + " FROM \"" + this.m_strName + "\" WHERE id = \"" + mediaIndex + "\"");
  276.       
  277.       this.m_queryObject.Execute();
  278.       this.m_queryObject.WaitForCompletion();
  279.       
  280.       var resObj = this.m_queryObject.GetResultObject();
  281.  
  282.       if(resObj.GetRowCount())
  283.         return resObj.GetRowCell(0, 0);
  284.     }
  285.         
  286.     return "";
  287.   },
  288.   
  289.   GetColumnValueByGUID: function(mediaGUID, strColumn)
  290.   {
  291.     if(this.m_queryObject != null)
  292.     {
  293.       this.m_queryObject.ResetQuery();
  294.       this.m_queryObject.AddQuery("SELECT " + strColumn + " FROM \"" + this.m_strName + "\" WHERE uuid = \"" + mediaGUID + "\"");
  295.       
  296.       this.m_queryObject.Execute();
  297.       this.m_queryObject.WaitForCompletion();
  298.       
  299.       var resObj = this.m_queryObject.GetResultObject();
  300.  
  301.       if(resObj.GetRowCount())
  302.         return resObj.GetRowCell(0, 0);    
  303.     }
  304.       
  305.     return "";
  306.   },
  307.  
  308.   GetColumnValuesByIndex: function(mediaIndex, nColumnCount, aColumns, nValueCount)
  309.   {
  310.     nValueCount = 0;
  311.     var aValues = new Array();
  312.  
  313.     if(this.m_queryObject != null)
  314.     {
  315.       var strQuery = "SELECT ";
  316.       this.m_queryObject.ResetQuery();
  317.       
  318.       var i = 0;
  319.       for( ; i < nColumnCount; i++)
  320.       {
  321.         strQuery += aColumns[i];
  322.         
  323.         if(i < nColumnCount - 1)
  324.           strQuery += ", ";
  325.       }
  326.       
  327.       strQuery += " FROM \"" + this.m_strName + "\" WHERE id = \"" + mediaIndex + "\"";
  328.       this.m_queryObject.AddQuery(strQuery);
  329.       
  330.       this.m_queryObject.Execute();
  331.       this.m_queryObject.WaitForCompletion();
  332.       
  333.       var resObj = this.m_queryObject.GetResultObject();
  334.       nValueCount = resObj.GetColumnCount();
  335.       
  336.       for(var i = 0; i < nValueCount; i++)
  337.       {
  338.         aValues.push(resObj.GetRowCell(0, i));
  339.       }    
  340.     }
  341.     
  342.     return aValues;
  343.   },
  344.   
  345.   GetColumnValuesByGUID: function(mediaGUID, nColumnCount, aColumns, nValueCount)
  346.   {
  347.     nValueCount = 0;
  348.     var aValues = new Array();
  349.  
  350.     if(this.m_queryObject != null)
  351.     {
  352.       var strQuery = "SELECT ";
  353.       this.m_queryObject.ResetQuery();
  354.       
  355.       var i = 0;
  356.       for( ; i < nColumnCount; i++)
  357.       {
  358.         strQuery += aColumns[i];
  359.         
  360.         if(i < nColumnCount - 1)
  361.           strQuery += ", ";
  362.       }
  363.       
  364.       strQuery += " FROM \"" + this.m_strName + "\" WHERE uuid = \"" + mediaGUID + "\"";
  365.       this.m_queryObject.AddQuery(strQuery);
  366.       
  367.       this.m_queryObject.Execute();
  368.       this.m_queryObject.WaitForCompletion();
  369.       
  370.       var resObj = this.m_queryObject.GetResultObject();
  371.       nValueCount = resObj.GetColumnCount();
  372.       
  373.       for(var i = 0; i < nValueCount; i++)
  374.       {
  375.         aValues.push(resObj.GetRowCell(0, i));
  376.       }    
  377.     }
  378.     
  379.     return aValues;
  380.   },
  381.   
  382.   SetColumnValueByIndex: function(mediaIndex, strColumn, strValue)
  383.   {
  384.     if(this.m_queryObject != null)
  385.     {
  386.       if(!bWillRunLater)
  387.         this.m_queryObject.ResetQuery();
  388.       
  389.       strValue = strValue.replace(/"/g, "\"\"");
  390.       this.m_queryObject.AddQuery("UPDATE library SET " + strColumn + " = \"" + strValue + "\" WHERE id = \"" + mediaIndex + "\"");
  391.       
  392.       if(!bWillRunLater)
  393.       {
  394.         this.m_queryObject.Execute();
  395.         this.m_queryObject.WaitForCompletion();
  396.       }
  397.     }
  398.     
  399.     return;
  400.   },
  401.   
  402.   SetColumnValueByGUID: function(mediaGUID, strColumn, strValue)
  403.   {
  404.     if(this.m_queryObject != null)
  405.     {
  406.       if(!bWillRunLater)
  407.         this.m_queryObject.ResetQuery();
  408.       
  409.       strValue = strValue.replace(/"/g, "\"\"");
  410.       this.m_queryObject.AddQuery("UPDATE library SET " + strColumn + " = \"" + strValue + "\" WHERE uuid = \"" + mediaGUID + "\"");
  411.       
  412.       if(!bWillRunLater)
  413.       {
  414.         this.m_queryObject.Execute();
  415.         this.m_queryObject.WaitForCompletion();
  416.       }
  417.     }
  418.     
  419.     return;
  420.   },
  421.  
  422.   SetColumnValuesByIndex: function(mediaIndex, nColumnCount, aColumns, nValueCount, aValues)
  423.   {
  424.     if(this.m_queryObject != null ||
  425.        nColumnCount != nValueCount)
  426.     {
  427.       if(!bWillRunLater)
  428.         this.m_queryObject.ResetQuery();
  429.       
  430.       var strQuery = "UPDATE library SET ";
  431.       var i = 0;
  432.       for(; i < nColumnCount; i++)
  433.       {
  434.         aValues[i] = aValues[i].replace(/"/g, "\"\"");
  435.         strQuery += aColumns[i] + " = \"" + aValues[i] + "\"";
  436.         if(i < nColumnCount - 1)
  437.           strQuery += ", ";
  438.       }
  439.       
  440.       strQuery += " WHERE id = \"" + mediaIndex + "\"";
  441.       this.m_queryObject.AddQuery(strQuery);
  442.       
  443.       if(!bWillRunLater)
  444.       {
  445.         this.m_queryObject.Execute();
  446.         this.m_queryObject.WaitForCompletion();
  447.       }
  448.     }
  449.  
  450.     return;
  451.   },
  452.   
  453.   SetColumnValuesByGUID: function(mediaGUID, nColumnCount, aColumns, nValueCount, aValues)
  454.   {
  455.     if(this.m_queryObject != null ||
  456.        nColumnCount != nValueCount)
  457.     {
  458.       if(!bWillRunLater)
  459.         this.m_queryObject.ResetQuery();
  460.       
  461.       var strQuery = "UPDATE library SET ";
  462.       var i = 0;
  463.       for(; i < nColumnCount; i++)
  464.       {
  465.         aValues[i] = aValues[i].replace(/"/g, "\"\"");
  466.         strQuery += aColumns[i] + " = \"" + aValues[i] + "\"";
  467.         if(i < nColumnCount - 1)
  468.           strQuery += ", ";
  469.       }
  470.       
  471.       strQuery += " WHERE uuid = \"" + mediaGUID + "\"";
  472.       this.m_queryObject.AddQuery(strQuery);
  473.       
  474.       if(!bWillRunLater)
  475.       {
  476.         this.m_queryObject.Execute();
  477.         this.m_queryObject.WaitForCompletion();
  478.       }
  479.     }
  480.  
  481.     return;
  482.   },
  483.   
  484.   SetName: function(strName)
  485.   {
  486.     this.m_strName = strName;
  487.     return;
  488.   },
  489.   
  490.   GetName: function()
  491.   {
  492.     return this.m_strName;
  493.   },
  494.   
  495.   SetReadableName: function(strReadableName)
  496.   {
  497.     this.m_queryObject.ResetQuery();
  498.     
  499.     strReadableName = strReadableName.replace(/"/g, "\"\"");
  500.     this.m_queryObject.AddQuery("UPDATE " + SMARTPLAYLIST_LIST_TABLE_NAME + " SET readable_name = \"" + strReadableName + "\" WHERE name = \"" + this.m_strName + "\"");
  501.     
  502.     this.m_queryObject.Execute();
  503.     this.m_queryObject.WaitForCompletion();
  504.     
  505.     return;
  506.   },
  507.   
  508.   GetReadableName: function()
  509.   {
  510.     var strReadableName = "";
  511.     this.m_queryObject.ResetQuery();
  512.     this.m_queryObject.AddQuery("SELECT readable_name FROM " + SMARTPLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + this.m_strName + "\"");
  513.     
  514.     this.m_queryObject.Execute();
  515.     this.m_queryObject.WaitForCompletion();
  516.     
  517.     var resObj = this.m_queryObject.GetResultObject();
  518.     
  519.     if(resObj.GetRowCount())
  520.       strReadableName = resObj.GetRowCell(0, 0);    
  521.     
  522.     return strReadableName;
  523.   },
  524.  
  525.   SetLibrary: function(strLibrary, bWillRunLater)
  526.   {
  527.     if(this.m_queryObject != null)
  528.     {
  529.       if(!bWillRunLater)
  530.         this.m_queryObject.ResetQuery();
  531.       
  532.       var strColumn = "library";
  533.       var strValue = strLibrary;
  534.       
  535.       this.m_queryObject.AddQuery("UPDATE \"" + SMARTPLAYLIST_LIST_TABLE_NAME + "\" SET " + strColumn + " = \"" + strValue + "\" WHERE name = \"" + this.m_strName + "\"");
  536.       
  537.       if(!bWillRunLater)
  538.       {
  539.         this.m_queryObject.Execute();
  540.         this.m_queryObject.WaitForCompletion();
  541.       }
  542.     }
  543.     
  544.     return;
  545.   },
  546.   
  547.   GetLibrary: function()
  548.   {
  549.     if(this.m_queryObject != null)
  550.     {
  551.       this.m_queryObject.ResetQuery();
  552.       
  553.       var strColumn = "library";
  554.       this.m_queryObject.AddQuery("SELECT " + strColumn + " FROM \"" + SMARTPLAYLIST_LIST_TABLE_NAME + "\" WHERE name = \"" + this.m_strName + "\"");
  555.       
  556.       this.m_queryObject.Execute();
  557.       this.m_queryObject.WaitForCompletion();
  558.       
  559.       var resObj = this.m_queryObject.GetResultObject();
  560.  
  561.       if(resObj.GetRowCount())
  562.         return resObj.GetRowCell(0, 0);    
  563.     }
  564.       
  565.     return "";
  566.   },
  567.  
  568.   SetLimitAndType: function(nLimit, strLimitType, bWillRunLater)
  569.   {
  570.     if(this.m_queryObject != null)
  571.     {
  572.       if(!bWillRunLater)
  573.         this.m_queryObject.ResetQuery();
  574.       
  575.       var strColumnA = "limit_value";
  576.       var strColumnB = "limit_type";
  577.       var strValueA = nLimit;
  578.       var strValueB = strLimitType;
  579.       
  580.       this.m_queryObject.AddQuery("UPDATE \"" + SMARTPLAYLIST_LIST_TABLE_NAME + "\" SET " + strColumnA + " = \"" + strValueA + "\", " + strColumnB + " = \"" + strValueB + "\" WHERE name = \"" + this.m_strName + "\"");
  581.       
  582.       if(!bWillRunLater)
  583.       {
  584.         this.m_queryObject.Execute();
  585.         this.m_queryObject.WaitForCompletion();
  586.       }
  587.     }
  588.     
  589.     return;
  590.   },
  591.   
  592.   GetLimit: function()
  593.   {
  594.     if(this.m_queryObject != null)
  595.     {
  596.       this.m_queryObject.ResetQuery();
  597.       
  598.       var strColumn = "limit_value";
  599.       this.m_queryObject.AddQuery("SELECT " + strColumn + " FROM \"" + SMARTPLAYLIST_LIST_TABLE_NAME + "\" WHERE name = \"" + this.m_strName + "\"");
  600.       
  601.       this.m_queryObject.Execute();
  602.       this.m_queryObject.WaitForCompletion();
  603.       
  604.       var resObj = this.m_queryObject.GetResultObject();
  605.  
  606.       if(resObj.GetRowCount())
  607.         return resObj.GetRowCell(0, 0);    
  608.     }
  609.       
  610.     return "";
  611.   },
  612.   
  613.   GetLimitType: function()
  614.   {
  615.     if(this.m_queryObject != null)
  616.     {
  617.       this.m_queryObject.ResetQuery();
  618.       
  619.       var strColumn = "limit_type";
  620.       this.m_queryObject.AddQuery("SELECT " + strColumn + " FROM \"" + SMARTPLAYLIST_LIST_TABLE_NAME + "\" WHERE name = \"" + this.m_strName + "\"");
  621.       
  622.       this.m_queryObject.Execute();
  623.       this.m_queryObject.WaitForCompletion();
  624.       
  625.       var resObj = this.m_queryObject.GetResultObject();
  626.  
  627.       if(resObj.GetRowCount())
  628.         return resObj.GetRowCell(0, 0);    
  629.     }
  630.       
  631.     return "";
  632.   },
  633.   
  634.   GetLimitAndType: function(nLimit, strLimitType)
  635.   {
  636.     nLimit.value = 0;
  637.     strLimitType.value = "";
  638.     
  639.     if(this.m_queryObject != null)
  640.     {
  641.       this.m_queryObject.ResetQuery();
  642.       
  643.       var strColumnA = "limit_value";
  644.       var strColumnB = "limit_type";
  645.       this.m_queryObject.AddQuery("SELECT " + strColumn + ", " + strColumnB + " FROM \"" + SMARTPLAYLIST_LIST_TABLE_NAME + "\" WHERE name = \"" + this.m_strName + "\"");
  646.       
  647.       this.m_queryObject.Execute();
  648.       this.m_queryObject.WaitForCompletion();
  649.       
  650.       var resObj = this.m_queryObject.GetResultObject();
  651.  
  652.       if(resObj.GetRowCount())
  653.       {
  654.         nLimit.value = resObj.GetRowCell(0, 0);
  655.         strLimitType.value = resObj.GetRowCell(0, 1);
  656.       }
  657.     }
  658.   },
  659.   
  660.   SetSelectedBy: function(strSelectedBy, bWillRunLater)
  661.   {
  662.     if(this.m_queryObject != null)
  663.     {
  664.       if(!bWillRunLater)
  665.         this.m_queryObject.ResetQuery();
  666.       
  667.       var strColumn = "selected_by";
  668.       var strValue = strSelectedBy;
  669.       
  670.       this.m_queryObject.AddQuery("UPDATE \"" + SMARTPLAYLIST_LIST_TABLE_NAME + "\" SET " + strColumn + " = \"" + strValue + "\" WHERE name = \"" + this.m_strName + "\"");
  671.       
  672.       if(!bWillRunLater)
  673.       {
  674.         this.m_queryObject.Execute();
  675.         this.m_queryObject.WaitForCompletion();
  676.       }
  677.     }
  678.     
  679.     return;
  680.   },
  681.   
  682.   GetSelectedBy: function()
  683.   {
  684.     if(this.m_queryObject != null)
  685.     {
  686.       this.m_queryObject.ResetQuery();
  687.       
  688.       var strColumn = "selected_by";
  689.       this.m_queryObject.AddQuery("SELECT " + strColumn + " FROM \"" + SMARTPLAYLIST_LIST_TABLE_NAME + "\" WHERE name = \"" + this.m_strName + "\"");
  690.       
  691.       this.m_queryObject.Execute();
  692.       this.m_queryObject.WaitForCompletion();
  693.       
  694.       var resObj = this.m_queryObject.GetResultObject();
  695.  
  696.       if(resObj.GetRowCount())
  697.         return resObj.GetRowCell(0, 0);    
  698.     }
  699.       
  700.     return "";
  701.   },
  702.  
  703.   SetMatch: function(strMatchOn, bWillRunLater)
  704.   {
  705.     if(this.m_queryObject != null)
  706.     {
  707.       if(!bWillRunLater)
  708.         this.m_queryObject.ResetQuery();
  709.       
  710.       var strColumn = "match_on";
  711.       var strValue = strMatchOn;
  712.       
  713.       this.m_queryObject.AddQuery("UPDATE \"" + SMARTPLAYLIST_LIST_TABLE_NAME + "\" SET " + strColumn + " = \"" + strValue + "\" WHERE name = \"" + this.m_strName + "\"");
  714.       
  715.       if(!bWillRunLater)
  716.       {
  717.         this.m_queryObject.Execute();
  718.         this.m_queryObject.WaitForCompletion();
  719.       }
  720.     }
  721.     
  722.     return;
  723.   },
  724.   
  725.   GetMatch: function()
  726.   {
  727.     if(this.m_queryObject != null)
  728.     {
  729.       this.m_queryObject.ResetQuery();
  730.       
  731.       var strColumn = "match_on";
  732.       this.m_queryObject.AddQuery("SELECT " + strColumn + " FROM \"" + SMARTPLAYLIST_LIST_TABLE_NAME + "\" WHERE name = \"" + this.m_strName + "\"");
  733.       
  734.       this.m_queryObject.Execute();
  735.       this.m_queryObject.WaitForCompletion();
  736.       
  737.       var resObj = this.m_queryObject.GetResultObject();
  738.  
  739.       if(resObj.GetRowCount())
  740.         return resObj.GetRowCell(0, 0);    
  741.     }
  742.       
  743.     return "";
  744.   },
  745.  
  746.   AddConstraint: function(strMetadata, strConstraint, strConstraintValue, bWillRunLater)
  747.   {
  748.     if(this.m_queryObject != null)
  749.     {
  750.       if(!bWillRunLater)
  751.         this.m_queryObject.ResetQuery();
  752.       
  753.       var strQuery = "INSERT INTO \"" + this.m_strName + "_constraints\" (column_name, constraint_name, constraint_value) VALUES (\"";
  754.       strQuery += strMetadata + "\", \"";
  755.       strQuery += strConstraint + "\", \"";
  756.       strQuery += strConstraintValue + "\")";
  757.       
  758.       this.m_queryObject.AddQuery(strQuery);
  759.       
  760.       if(!bWillRunLater)
  761.       {
  762.         this.m_queryObject.Execute();
  763.         this.m_queryObject.WaitForCompletion();
  764.       }
  765.     }
  766.   },
  767.   
  768.   GetConstraintCount: function()
  769.   {
  770.     if(this.m_queryObject != null)
  771.     {
  772.       this.m_queryObject.ResetQuery();
  773.       this.m_queryObject.AddQuery("SELECT COUNT(id) FROM \"" + this.m_strName + "_constraints\"");
  774.       
  775.       this.m_queryObject.Execute();
  776.       this.m_queryObject.WaitForCompletion();
  777.       
  778.       var resObj = this.m_queryObject.GetResultObject();
  779.  
  780.       if(resObj.GetRowCount())
  781.         return resObj.GetRowCell(0, 0);    
  782.     }
  783.       
  784.     return 0;
  785.   },
  786.   
  787.   GetAllConstraints: function()
  788.   {
  789.     if(this.m_queryObject != null)
  790.     {
  791.       this.m_queryObject.ResetQuery();
  792.       this.m_queryObject.AddQuery("SELECT * FROM \"" + this.m_strName + "_constraints\"" );
  793.       
  794.       this.m_queryObject.Execute();
  795.       this.m_queryObject.WaitForCompletion();
  796.       
  797.       var resObj = this.m_queryObject.GetResultObject();
  798.       return resObj.GetRowCount();
  799.     }
  800.     
  801.     return 0;
  802.   },
  803.   
  804.   GetConstraint: function(nIndex)
  805.   {
  806.     if(this.m_queryObject != null)
  807.     {
  808.       this.m_queryObject.ResetQuery();
  809.       this.m_queryObject.AddQuery("SELECT * FROM \"" + this.m_strName + "_constraints\" WHERE id = " + nIndex);
  810.       
  811.       this.m_queryObject.Execute();
  812.       this.m_queryObject.WaitForCompletion();
  813.       
  814.       var resObj = this.m_queryObject.GetResultObject();
  815.       if(resObj.GetRowCount())
  816.         return true;
  817.     }
  818.     
  819.     return false;
  820.   },
  821.   
  822.   ReplaceConstraint: function(nIndex, strMetadata, strConstraint, strConstraintValue, bWillRunLater)
  823.   {
  824.     if(this.m_queryObject != null)
  825.     {
  826.       if(!bWillRunLater)
  827.         this.m_queryObject.ResetQuery();
  828.       
  829.       var strQuery = "INSERT OR REPLACE INTO \"" + this.m_strName + "_constraints\" VALUES (\"";
  830.       strQuery += nIndex + "\", \"";
  831.       strQuery += strMetadata + "\", \"";
  832.       strQuery += strConstraint + "\", \"";
  833.       strQuery += strConstraintValue + "\")";
  834.       
  835.       this.m_queryObject.AddQuery(strQuery);
  836.       
  837.       if(!bWillRunLater)
  838.       {
  839.         this.m_queryObject.Execute();
  840.         this.m_queryObject.WaitForCompletion();
  841.       }
  842.     }
  843.   },
  844.   
  845.   RemoveConstraint: function(nIndex, bWillRunLater)
  846.   {
  847.     if(this.m_queryObject != null)
  848.     {
  849.       if(!bWillRunLater)
  850.         this.m_queryObject.ResetQuery();
  851.         
  852.       this.m_queryObject.AddQuery("DELETE FROM \"" + this.m_strName + "_constraints\" WHERE id = " + nIndex);
  853.       
  854.       if(!bWillRunLater)
  855.       {
  856.         this.m_queryObject.Execute();
  857.         this.m_queryObject.WaitForCompletion();
  858.       }
  859.     }
  860.     
  861.     return;
  862.   },
  863.  
  864.   RemoveAllConstraints: function(bWillRunLater)
  865.   {
  866.     if(this.m_queryObject != null)
  867.     {
  868.       if(!bWillRunLater)
  869.         this.m_queryObject.ResetQuery();
  870.         
  871.       this.m_queryObject.AddQuery("DELETE FROM \"" + this.m_strName + "_constraints\"");
  872.       
  873.       if(!bWillRunLater)
  874.       {
  875.         this.m_queryObject.Execute();
  876.         this.m_queryObject.WaitForCompletion();
  877.       }
  878.     }
  879.     
  880.     return;
  881.   },
  882.  
  883.   SetQuery: function(strQuery, bWillRunLater)
  884.   {
  885.     if(this.m_queryObject != null)
  886.     {
  887.       if(!bWillRunLater)
  888.         this.m_queryObject.ResetQuery();
  889.       
  890.       var strColumn = "query";
  891.       var strValue = strQuery;
  892.       
  893.       this.m_queryObject.AddQuery("UPDATE \"" + SMARTPLAYLIST_LIST_TABLE_NAME + "\" SET " + strColumn + " = \"" + strValue + "\" WHERE name = \"" + this.m_strName + "\"");
  894.       
  895.       if(!bWillRunLater)
  896.       {
  897.         this.m_queryObject.Execute();
  898.         this.m_queryObject.WaitForCompletion();
  899.       }
  900.     }
  901.     
  902.     return;
  903.   },
  904.   
  905.   GetQuery: function()
  906.   {
  907.     return this.m_strQuery;
  908.   },
  909.   
  910.   RebuildPlaylist: function()
  911.   {
  912.     if(this.m_queryObject != null)
  913.     {
  914.       var resObj = null;
  915.       var strQuery = "";
  916.       var strTheSmartQuery = "SELECT * FROM library";
  917.  
  918.       strQuery = "SELECT * FROM \"" + SMARTPLAYLIST_LIST_TABLE_NAME + "\" WHERE name = \"" + this.m_strName + "\"";
  919.       this.m_queryObject.ResetQuery();
  920.       this.m_queryObject.AddQuery(strQuery);
  921.       this.m_queryObject.Execute();
  922.       this.m_queryObject.WaitForCompletion();
  923.  
  924.       resObj = this.m_queryObject.GetResultObject();
  925.       
  926.       var library = resObj.GetRowCellByColumn(0, "library");
  927.       var limitValue = resObj.GetRowCellByColumn(0, "limit_value");
  928.       var limitType = resObj.GetRowCellByColumn(0, "limit_type");
  929.       var selectedBy = resObj.GetRowCellByColumn(0, "selected_by");
  930.       var match = resObj.GetRowCellByColumn(0, "match_on");
  931.       
  932.       if(match == "any") match = "OR";
  933.       else if(match == "all") match = "AND";
  934.       
  935.       strQuery = "SELECT * FROM \"" + this.m_strName + "_constraints\"";
  936.       this.m_queryObject.ResetQuery();
  937.       this.m_queryObject.AddQuery(strQuery);
  938.       this.m_queryObject.Execute();
  939.       this.m_queryObject.WaitForCompletion();
  940.       
  941.       resObj = this.m_queryObject.GetResultObject();
  942.      
  943.       var rowCount = resObj.GetRowCount();
  944.       
  945.       if(rowCount)
  946.         strTheSmartQuery += " WHERE ";
  947.       
  948.       var constraint = "", constraintValue = "", i = 0;
  949.       for(; i < rowCount; ++i)
  950.       {
  951.         strTheSmartQuery += resObj.GetRowCellByColumn(i, "column_name") + " ";
  952.         constraint = resObj.GetRowCellByColumn(i, "constraint_name");
  953.         constraintValue = resObj.GetRowCellByColumn(i, "constraint_value");
  954.         
  955.         switch(constraint)
  956.         {
  957.           case "contains": strTheSmartQuery += "LIKE \"%" + constraintValue + "%\""; break;
  958.           case "does not contain": strTheSmartQuery += "NOT LIKE \"%" + constraintValue + "%\""; break;
  959.           case "is": strTheSmartQuery += "= \"" + constraintValue + "\""; break;
  960.           case "is not": strTheSmartQuery += "!= \"" + constraintValue + "\""; break;
  961.           case "starts with": strTheSmartQuery += "LIKE \"" + constraintValue + "%\""; break;
  962.           case "ends with": strTheSmartQuery += "LIKE \"%" + constraintValue + "\""; break;
  963.         }
  964.         
  965.         if(i != rowCount - 1)
  966.           strTheSmartQuery += " " + match + " ";
  967.       }
  968.       
  969.       if(limitValue != 0)
  970.       {
  971.       }
  972.       
  973.       this.m_strQuery = strTheSmartQuery;
  974.       
  975.       this.m_queryObject.ResetQuery();
  976.       
  977.       strQuery = "UPDATE OR IGNORE \"" + SMARTPLAYLIST_LIST_TABLE_NAME + "\" SET query = x\"" + strTheSmartQuery + "\" WHERE name = \"" + this.m_strName + "\"";
  978.       
  979.       this.m_queryObject.AddQuery(strQuery);
  980.       
  981.       this.m_queryObject.AddQuery("DROP VIEW \"" + this.m_strName + "\"")
  982.       this.m_queryObject.AddQuery("CREATE VIEW \"" + this.m_strName + "\" AS " + this.m_strQuery);
  983.       
  984.       this.m_queryObject.Execute();
  985.       this.m_queryObject.WaitForCompletion();
  986.       
  987.       return true; 
  988.     }
  989.     
  990.     return false;    
  991.   },
  992.   
  993.   QueryInterface: function(iid)
  994.   {
  995.       if (!iid.equals(Components.interfaces.nsISupports) &&
  996.           !iid.equals(SONGBIRD_SMARTPLAYLIST_IID) && 
  997.           !iid.equals(SONGBIRD_PLAYLIST_IID))
  998.           throw Components.results.NS_ERROR_NO_INTERFACE;
  999.       return this;
  1000.   }
  1001. };
  1002.  
  1003. /**
  1004.  * \class sbSmartPlaylistModule
  1005.  * \brief 
  1006.  */
  1007. var sbSmartPlaylistModule = 
  1008. {
  1009.   registerSelf: function(compMgr, fileSpec, location, type)
  1010.   {
  1011.       compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  1012.       compMgr.registerFactoryLocation(SONGBIRD_SMARTPLAYLIST_CID, 
  1013.                                       SONGBIRD_SMARTPLAYLIST_CLASSNAME, 
  1014.                                       SONGBIRD_SMARTPLAYLIST_CONTRACTID, 
  1015.                                       fileSpec, 
  1016.                                       location,
  1017.                                       type);
  1018.   },
  1019.  
  1020.   getClassObject: function(compMgr, cid, iid) 
  1021.   {
  1022.       if (!cid.equals(SONGBIRD_SMARTPLAYLIST_CID))
  1023.           throw Components.results.NS_ERROR_NO_INTERFACE;
  1024.  
  1025.       if (!iid.equals(Components.interfaces.nsIFactory))
  1026.           throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  1027.  
  1028.       return sbSmartPlaylistFactory;
  1029.   },
  1030.  
  1031.   canUnload: function(compMgr)
  1032.   { 
  1033.     return true; 
  1034.   }
  1035. }; //sbSmartPlaylistModule
  1036.  
  1037. /**
  1038.  * \class sbSmartPlaylistFactory
  1039.  * \brief 
  1040.  */
  1041. var sbSmartPlaylistFactory =
  1042. {
  1043.     createInstance: function(outer, iid)
  1044.     {
  1045.         if (outer != null)
  1046.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  1047.     
  1048.         if (!iid.equals(SONGBIRD_SMARTPLAYLIST_IID) &&
  1049.             !iid.equals(Components.interfaces.nsISupports))
  1050.             throw Components.results.NS_ERROR_INVALID_ARG;
  1051.  
  1052.         return (new CSmartPlaylist()).QueryInterface(iid);
  1053.     }
  1054. }; //sbSmartPlaylistFactory
  1055.  
  1056. /**
  1057.  * \function NSGetModule
  1058.  * \brief 
  1059.  */
  1060. function NSGetModule(comMgr, fileSpec)
  1061.   return sbSmartPlaylistModule;
  1062. } //NSGetModule